<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：loan.class.php
 * @类功能: 贷款产品
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\api\controller;

use app\admin\widget\Name;
use app\common\cache\agent\IAgentAdminCache;
use app\common\cache\loan\ILoanAgencyCache;
use app\common\cache\loan\ILoanFavoriteCache;
use app\common\cache\loan\ILoanCache;
use app\common\cache\loan\ILoanRecommendCache;
use app\common\cache\user\IUserCache;
use app\common\controller\ApiBaseController;
use app\common\enum\IsHistory;
use app\common\enum\IsTrack;
use app\common\enum\LoanApplyStatus;
use app\common\enum\LoanMaterialStatus;
use app\common\enum\LoanStatus;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsDelete;
use app\common\enum\MaterialStatus;
use app\common\enum\QuestionType;
use app\common\ext\IDb;
use app\common\ext\ILog;
use app\common\ext\ITree;
use app\common\ext\IUrl;
use app\common\ext\UserSyncLink;
use app\common\ext\User;

class Loan extends ApiBaseController{
    /**
     * @功能：取得推荐贷款产品
     * @开发者：cxl
     */
    public function recommend(){
        //接收参数
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编号不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编号不能为空！");
        $recommend_location = self::get_data('recommend_location',self::NOT_EMPTY,"推荐位置不能为空！");

        //判断列表是否存在
        $loan_recommend_list = ILoanRecommendCache::getList($recommend_location,$province_id,$city_id);
        if($loan_recommend_list === false){
            self::set_code(self::ERR_0001);
            self::set_msg("取得推荐贷款产品列表失败！");
            self::send();
        }

        //判断列表是否存在
        if(!empty($loan_recommend_list) && is_array($loan_recommend_list)) {
            //整理银行图片信息
            foreach ($loan_recommend_list as &$data) {
                //数据整理
                $data['loan_product_name'] = "{$data['loan_agency_name']}-{$data['loan_product_name']}";//设置产品名称
                $data['loan_range'] = number_format($data['min_loan_money'],1).'~'.number_format($data['max_loan_money'],1);
//                $data['loan_product_note'] = number_format($data['min_loan_money'],1)."万-".number_format($data['max_loan_money'],1)."万 ";//设置产品说明

                //取得标签
                if(!empty($data['loan_tag_str'])){
                    $data['loan_tag'] = explode(',',$data['loan_tag_str']);
                }else{
                    $data['loan_tag'] = array();
                }
//
                //删除多余数据
                unset($data['loan_agency_name'],$data['min_loan_money'],$data['max_loan_money'],$data['loan_repayment'],$data['loan_tag_str']);
            }
        }else{
            $loan_recommend_list = array();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_recommend_list",$loan_recommend_list);
        self::send();
    }

    /**
     * @功能：贷款产品搜索选项
     * @开发者：cxl
     */
    public function loan_search(){
        //判断类型数据
        $search_type = array(
            0 => array("search_type" => 1,"search_type_name"=>"不限抵押类型","search_item"=>array()),
            1 => array("search_type" => 2,"search_type_name"=>"默认排序","search_item"=>array()),
            2 => array("search_type" => 3,"search_type_name"=>"更多","search_item"=>array()),
        );

        //取得名次
        $search_type_name = array(
            "loan_pledge"=>"抵押方式",
            "loan_repayment"=>"还款方式",
            "loan_order"=>"排序方式",
            "loan_period"=>"贷款期限",
            "loan_identity"=>"职业身份",
//            "loan_agency_type"=>"放款机构类型",
        );

        //设置用途
        $search = array(
            array("search_type"=>0,"name"=>"loan_pledge"),
            array("search_type"=>1,"name"=>"loan_order",),
            array("search_type"=>2,"name"=>"loan_repayment"),
            array("search_type"=>2,"name"=>"loan_period",),
            array("search_type"=>2,"name"=>"loan_identity",),
//            array("search_type"=>2,"name"=>"loan_agency_type",),
        );

        //循环添加数据
        foreach($search as $item){
            //初期化变量
            $data = array();
            $data['filed'] = $item['name'];
            $data['name'] = $search_type_name[$item['name']];

            //判断数据是否存在
            if(!empty(HtmlEnumValue::$enum_value[$item['name']]) && is_array(HtmlEnumValue::$enum_value[$item['name']])){
                //设置初期値
                $loan_item_data = HtmlEnumValue::$enum_value[$item['name']];

                //设置值
                foreach($loan_item_data as $key=>$val){
                    //判断说明是否存在
                    if(empty($item['note']) || empty($item['note'][$key])){
                        $data['value'][] = array("item" => $key, "title" => $val,"note" => "");
                    }else{
                        $data['value'][] = array("item" => $key, "title" => $val, "note" => $item['note'][$key]);
                    }
                }
            }

            //设置用途
            $search_type[$item['search_type']]['search_item'][] = $data;
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_loan_item_list",$search_type);
        self::send();
    }

    /**
     * @功能：搜索贷款产品
     * @开发者：cxl
     */
    public function loan_search_list(){
        //接收参数
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编号不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编号不能为空！");
        $loan_pledge  = self::get_data('loan_pledge');
        $loan_repayment  = self::get_data('loan_repayment');
        $loan_period  = self::get_data('loan_period');
        $loan_identity  = self::get_data('loan_identity');
//        $loan_agency_type  = self::get_data('loan_agency_type');
        $loan_order  = self::get_data('loan_order');
        $bottom_id  = self::get_data('bottom_id');

        //查询条件
        $loan_product_where['ct.is_delete'] = IsDelete::No;
        $loan_product_where['ct.loan_status'] = LoanStatus::release;
        if(!empty($loan_pledge)) $loan_product_where['ct.loan_pledge'] = $loan_pledge;
        if(!empty($loan_repayment)) $loan_product_where['ct.loan_repayment'] = $loan_repayment;
        if(!empty($loan_period)) {
            //array(1=>"3个月",2=>"6个月",3=>"12个月",4=>"2年",5=>"3年",6=>"5年",7=>"10年")
            //参考app/common/enum/tmlEnumValue.php中的贷款期限 "loan_period" 转化为月
            $period_map = array(1=>"3",2=>"6",3=>"12",4=>"24",5=>"36",6=>"60",7=>"120");
            $loan_product_where['ct.min_period'] = ['<=',$period_map[$loan_period]];
            $loan_product_where['ct.max_period'] = ['>=',$period_map[$loan_period]];
        }
        if(!empty($loan_identity)) $loan_product_where[] = array("exp","find_in_set($loan_identity,loan_identity_list)");
//        if(!empty($loan_agency_type)) $loan_product_where['ct.loan_agency_type'] = $loan_agency_type;

        //查询记录
        $loan_search_list = ILoanCache::getList($loan_product_where,$loan_order,$province_id,$city_id,$bottom_id);

        //判断列表是否存在
        if(!empty($loan_search_list) && is_array($loan_search_list)) {
            //整理银行图片信息
            foreach ($loan_search_list as &$data) {
                //数据整理
                $data['loan_product_name'] = "{$data['loan_agency_name']}-{$data['loan_product_name']}";//设置产品名称
                $data['loan_range'] = number_format($data['min_loan_money'],1).'~'.number_format($data['max_loan_money'],1);
//                $data['loan_product_note'] = number_format($data['min_loan_money'],1)."万-".number_format($data['max_loan_money'],1)."万 ";//设置产品说明

                //取得标签
                if(!empty($data['loan_tag_str'])){
                    $data['loan_tag'] = explode(',',$data['loan_tag_str']);
                }else{
                    $data['loan_tag'] = array();
                }
//
                //删除多余数据
                unset($data['loan_agency_name'],$data['min_loan_money'],$data['max_loan_money'],$data['loan_repayment'],$data['loan_tag_str']);
            }
        }else{
            $loan_search_list = array();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_search_list",$loan_search_list);
        self::send();
    }

    /**
     * @功能：设置分享记录
     * @开发者：cxl
     */
    public function loan_share_log(){
        //接收参数
        $user_id = self::$uid;
        $share_type  = self::get_data('share_type',self::NOT_EMPTY,"分享类型不能为空！");
        $loan_product_id  = self::get_data('loan_product_id',self::NOT_EMPTY,"文章资讯编号不能为空！");

        //设置分享记录
        $loan_share_data['share_type'] = $share_type;
        if(!empty($user_id))$loan_share_data['user_id'] = $user_id;
        $loan_share_data['loan_product_id'] = $loan_product_id;
        $loan_share_add = IDb::getInstance("loan_share")->setDbData($loan_share_data)->add();
        if($loan_share_add === false){
            self::set_code(self::ERR_0001);
            self::set_msg("设置分享日志失败！");
            self::send();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：贷款产品明细信息
     * @开发者：cxl
     */
    public function loan_details(){
        //接收参数
        $user_id = self::$uid;
        $loan_id  = self::get_data('loan_id',self::NOT_EMPTY,"贷款产品编码不能为空！");

        //取得银行贷详情信息
        $loan_product_info = ILoanCache::getInfo($loan_id);

        //判断银行贷信息是否已经删除
        if($loan_product_info['is_delete'] == IsDelete::Yes){
            self::set_code(self::WARNING_0001);
            self::set_msg("银行贷尚未发布或者已经下架！");
            self::send();
        }

        //判断贷款产品信息是否已经下降
        if($loan_product_info['loan_status'] != LoanStatus::release){
            self::set_code(self::WARNING_0002);
            self::set_msg("贷款产品尚未发布或者已经下架！");
            self::send();
        }

        //设置产品名称
        if(!empty($loan_agency_info['loan_agency_name'])){
            $loan_product_info['loan_product_name'] = "{$loan_product_info['loan_agency_name']}-{$loan_product_info['loan_product_name']}";
//            $loan_product_info['loan_product_url'] = $loan_agency_info['thumb'];
        }

        //整理状态数据
        if(!empty($loan_product_info['loan_pledge'])) {$loan_product_info['loan_pledge'] = HtmlEnumValue::$enum_value['loan_pledge'][$loan_product_info['loan_pledge']];}else{$loan_product_info['loan_pledge'] = '';} //抵押方式
        if(!empty($loan_product_info['loan_repayment'])) {$loan_product_info['loan_repayment'] = HtmlEnumValue::$enum_value['loan_repayment'][$loan_product_info['loan_repayment']];}else{$loan_product_info['loan_repayment'] = '';} //还款方式
        if(!empty($loan_product_info['credit_record'])) {$loan_product_info['credit_record'] = HtmlEnumValue::$enum_value['credit_record'][$loan_product_info['credit_record']];}else{$loan_product_info['credit_record'] = '';} //信用记录
        if(!empty($loan_product_info['is_house'])) {$loan_product_info['is_house'] = HtmlEnumValue::$enum_value['is_house'][$loan_product_info['is_house']];}else{$loan_product_info['is_house'] = '';} //本市是否有房产

        //整理json数据
//        if(!empty($loan_product_info['loan_apply_condition_str'])) {$loan_product_info['loan_apply_condition_str'] = json_decode($loan_product_info['loan_apply_condition_str'],true);}else{$loan_product_info['loan_apply_condition_str'] = array();} //申请条件
//        if(!empty($loan_product_info['loan_apply_material_str'])) {$loan_product_info['loan_apply_material_str'] = json_decode($loan_product_info['loan_apply_material_str'],true);}else{$loan_product_info['loan_apply_material_str'] = array();} //申请材料(JSON)

        //处理标签信息
        if(!empty($loan_product_info['loan_tag_str'])) {
            //取得标签字符串
            $loan_product_info['loan_tag_str'] = explode(",",$loan_product_info['loan_tag_str']);
        }else{
            $loan_product_info['loan_tag_str'] = array();
        }

        //判断是否收藏
        if(empty($user_id)){
            $loan_product_info['is_favorite'] = 1;
        }else{
            //查询是否收藏
            $loan_favorite_where['is_delete'] = IsDelete::No;
            $loan_favorite_where['user_id'] = $user_id;
            $loan_favorite_where['loan_product_id'] = $loan_id;
            $loan_favorite_info = IDb::getInstance('loan_favorite')->setDbFiled("loan_favorite_id")->setDbWhere($loan_favorite_where)->row();
            if(!empty($loan_favorite_info['loan_favorite_id'])){
                $loan_product_info['is_favorite'] = 2;
            }else{
                $loan_product_info['is_favorite'] = 1;
            }
        }

        //取得贷款产品分享地址
        $loan_product_info['share_url'] = IUrl::get("/wap/share/loan/loan_id/{$loan_id}.html");

        $loan_product_info['agent_admin_info'] = IAgentAdminCache::getInfoForDisplay($loan_product_info['agent_admin_id']);

        $loan_product_info['agent_admin_list'] = [$loan_product_info['agent_admin_info']];

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($loan_product_info);
        self::send();
    }

    /**
     * @功能：设置贷款产品收藏
     * @开发者：cxl
     */
    public function loan_favorite(){
        //接收参数
        $user_id  = self::$uid;
        $loan_id  = self::get_data('loan_id',self::NOT_EMPTY,"贷款产品编码不能为空！");

        //实例化对象
        $loan_product_info = ILoanCache::getInfo($loan_id);
        if(empty($loan_product_info['loan_product_id'])){
            self::set_code(self::WARNING_0001);
            self::set_msg("贷款产品不存在！");
            self::send();
        }

        //判断是否已经收藏
        $loan_favorite_where['user_id'] = $user_id;
        $loan_favorite_where['loan_product_id'] = $loan_id;
        $loan_favorite_info = IDb::getInstance("loan_favorite")->setDbFiled("loan_favorite_id")->setDbWhere($loan_favorite_where)->row();
        if(empty($loan_favorite_info['loan_favorite_id'])) {
            //添加收藏信息
            $loan_favorite_data['user_id'] = $user_id;
            $loan_favorite_data['loan_product_id'] = $loan_id;
            $loan_favorite_add = IDb::getInstance("loan_favorite")->setDbData($loan_favorite_data)->add();
            if($loan_favorite_add === false) {
                self::set_code(self::WARNING_0002);
                self::set_msg("收藏失败！");
                self::send();
            }
        }else{
            //添加收藏信息
            $loan_favorite_data['is_delete'] = IsDelete::No;
            $loan_favorite_upd = IDb::getInstance("loan_favorite")->setDbData($loan_favorite_data)->setDbWhere($loan_favorite_where)->upd();
            if($loan_favorite_upd === false) {
                self::set_code(self::WARNING_0002);
                self::set_msg("收藏失败！");
                self::send();
            }

            //收藏编号
            $loan_favorite_add = $loan_favorite_info['loan_favorite_id'];
        }

        //清除缓存
        ILoanFavoriteCache::deleteList($user_id);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_favorite_id",intval($loan_favorite_add));
        self::send();
    }

    /**
     * @功能：取消贷款产品收藏
     * @开发者：cxl
     */
    public function loan_favorite_cancel(){
        //接收参数
        $user_id  = self::$uid;
        $loan_id  = self::get_data('loan_id',self::NOT_EMPTY,"贷款产品编码不能为空！");

        //实例化对象
        $loan_product_info = ILoanCache::getInfo($loan_id);
        if(empty($loan_product_info['loan_product_id'])){
            self::set_code(self::WARNING_0001);
            self::set_msg("贷款产品不存在！");
            self::send();
        }

        //添加收藏信息
        $loan_favorite_where['user_id'] = $user_id;
        $loan_favorite_where['loan_product_id'] = $loan_id;
        $loan_favorite_data['is_delete'] = IsDelete::Yes;
        $loan_favorite_upd = IDb::getInstance("loan_favorite")->setDbData($loan_favorite_data)->setDbWhere($loan_favorite_where)->upd();
        if($loan_favorite_upd === false) {
            self::set_code(self::WARNING_0002);
            self::set_msg("收藏失败！");
            self::send();
        }

        //清除缓存
        ILoanFavoriteCache::deleteList($user_id);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：用户收藏贷款产品列表
     * @开发者：cxl
     */
    public function user_loan_favorite(){
        //接收参数
        $user_id  = self::$uid;
        $bottom_id  = self::get_data('bottom_id');

        //判断列表是否存在
        $loan_list = ILoanFavoriteCache::getList($user_id,$bottom_id);
        if(!empty($loan_list) && is_array($loan_list)) {
            //整理银行图片信息
            foreach ($loan_list as &$data) {
                //数据整理
                $data['loan_product_name'] = "{$data['loan_agency_name']}-{$data['loan_product_name']}";//设置产品名称
                $data['loan_range'] = number_format($data['min_loan_money'],1).'~'.number_format($data['max_loan_money'],1);

                //取得标签
                if(!empty($data['loan_tag_str'])){
                    $data['loan_tag'] = explode(',',$data['loan_tag_str']);
                }else{
                    $data['loan_tag'] = array();
                }

                //删除多余数据
                unset($data['apply_count'],$data['loan_agency_name'],$data['min_loan_money'],$data['max_loan_money'],$data['loan_repayment'],$data['loan_tag_str']);
            }
        }else{
            $loan_list = array();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_list",$loan_list);
        self::send();
    }

    /**
     * @功能：设置贷款产品申请
     * @开发者：cxl
     */
    public function loan_apply(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $loan_id  = self::get_data('loan_id',self::NOT_EMPTY,"贷款产品编码不能为空！");
        $province_id  = self::get_data('province_id',self::NOT_EMPTY,"省份编码不能为空！");
        $city_id  = self::get_data('city_id',self::NOT_EMPTY,"城市编码不能为空！");
        $apply_money  = self::get_data('apply_money',self::NOT_EMPTY,"申请金额不能为空！");
        $apply_period  = self::get_data('apply_period',self::NOT_EMPTY,"申请周期不能为空！");

        //判断产品是否存在
        $loan_product_info = ILoanCache::getInfo($loan_id);
        if(empty($loan_product_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("贷款产品申请失败！");
            self::send();
        }

        //判断产品是否上架
        if($loan_product_info['loan_status'] != LoanStatus::release){
            self::set_code(self::WARNING_0002);
            self::set_msg("贷款产品未上架或者已经下架！");
            self::send();
        }

        //开启事物出来
        IDb::dbStartTrans();

        //判断是否需要修改用户信息
        if(!empty($user_data)){
            $user_where['user_id'] = $user_id;
            $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
            if($user_upd === false){
                IDb::dbRollback();
                self::set_code(self::ERR_0001);
                self::set_msg("贷款产品申请失败！");
                self::send();
            }
        }

        //取得贷款产品信息
        $loan_apply_where['user_id'] = $user_id;
        $loan_apply_where['loan_product_id'] = $loan_id;
        $loan_apply_where['province_id'] = $province_id;
        $loan_apply_where['city_id'] = $city_id;
        $loan_apply_where['loan_material_status'] = LoanMaterialStatus::edit;
        $loan_apply_info = IDb::getInstance("loan_apply")->setDbWhere($loan_apply_where)->row();
        if(empty($loan_apply_info)){
            //设置贷款产品申请日志
            $loan_apply_data['agent_id'] = $loan_product_info['agent_id'];
            $loan_apply_data['agent_admin_id'] = $loan_product_info['agent_admin_id'];
            $loan_apply_data['loan_apply_type'] = 1;
            $loan_apply_data['user_id'] = $user_id;
            $loan_apply_data['loan_product_id'] = $loan_id;
            $loan_apply_data['province_id'] = $province_id;
            $loan_apply_data['city_id'] = $city_id;
            $loan_apply_data['apply_money'] = $apply_money;
            $loan_apply_data['apply_period'] = $apply_period;
            $loan_apply_data['loan_apply_time'] = get_date_time();
            $loan_apply_add = IDb::getInstance("loan_apply")->setDbData($loan_apply_data)->add();
            if($loan_apply_add === false){
                IDb::dbRollback();
                self::set_code(self::ERR_0003);
                self::set_msg("贷款产品申请失败！");
                self::send();
            }

            //写贷款产品日志
            $loan_apply_log_add = ILog::LoanApplyLog($loan_apply_add,$user_id,null,null,"申请贷款产品！");
            if ($loan_apply_log_add === false) {
                IDb::dbRollback();
                self::set_code(self::ERR_0004);
                self::set_msg("贷款产品申请失败！");
                self::send();
            }
        }else{
            self::set_code(self::WARNING_0001);
            self::set_msg("贷款已经申请过了！");
            self::send();
            //取得申请编号
//            $loan_apply_add = $loan_apply_info['loan_apply_id'];
//
//            //设置贷款产品申请日志
//            $loan_apply_where['loan_apply_id'] = $loan_apply_add;
//            $loan_apply_data['loan_apply_type'] = 1;
//            $loan_apply_data['user_id'] = $user_id;
//            $loan_apply_data['loan_product_id'] = $loan_id;
//            $loan_apply_data['province_id'] = $province_id;
//            $loan_apply_data['city_id'] = $city_id;
//            $loan_apply_data['apply_money'] = $apply_money;
//            $loan_apply_data['apply_period'] = $apply_period;
//            $loan_apply_data['loan_apply_time'] = get_date_time();
//            $loan_apply_upd = IDb::getInstance("loan_apply")->setDbWhere($loan_apply_where)->setDbData($loan_apply_data)->upd();
//            if($loan_apply_upd === false){
//                IDb::dbRollback();
//                self::set_code(self::ERR_0005);
//                self::set_msg("贷款产品申请失败！");
//                self::send();
//            }
//
//            //写贷款产品日志
//            $loan_apply_log_add = ILog::LoanApplyLog($loan_apply_add,$user_id,null,null,"修改申请贷款产品！");
//            if ($loan_apply_log_add === false) {
//                IDb::dbRollback();
//                self::set_code(self::ERR_0006);
//                self::set_msg("贷款产品申请失败！");
//                self::send();
//            }
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        ILoanCache::deleteInfo($loan_id);
        ILoanCache::deleteList();
        ILoanRecommendCache::deleteList();
        ILoanFavoriteCache::deleteListAll();
        IUserCache::deleteInfo($user_id);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_apply_id",$loan_apply_add);
        self::send();
    }

    /**
     * @功能：贷款申请提交个人信息
     * @开发者：gys
     */
    public function loan_apply_submit(){
        //接收参数
        $user_id = self::$uid;
        $loan_apply_id  = self::get_data('loan_apply_id',self::NOT_EMPTY,"申请编码不能为空！");
        $phone_info  = self::get_data('phone_info',self::NOT_EMPTY,"手机信息不能为空！");
        $user_data['is_social'] = self::get_data('is_social');
        $user_data['is_accumulation_fund'] = self::get_data('is_accumulation_fund');
        $user_data['is_overdue'] = self::get_data('is_overdue');
        $user_data['is_white'] = self::get_data('is_white');
        $user_data['is_court'] = self::get_data('is_court');
        $user_data['is_company'] = self::get_data('is_company');
        $user_data['is_house'] = self::get_data('is_house');
        $user_data['is_credit_card'] = self::get_data('is_credit_card');
        $user_data['is_policy'] = self::get_data('is_policy');
        $user_data['is_car'] = self::get_data('is_car');
        $user_data['is_loan'] = self::get_data('is_loan');
        $user_data['is_assure'] = self::get_data('is_assure');
        $user_data['is_shop'] = self::get_data('is_shop');

        //其它信息
        $user_data['occupation'] = self::get_data('occupation');
        $user_data['domicile_address'] = self::get_data('domicile_address');
        $user_data['work_address'] = self::get_data('work_address');
        $user_data['is_marry'] = self::get_data('is_marry');
        $user_data['couple_name'] = self::get_data('couple_name');
        $user_data['couple_phone'] = self::get_data('couple_phone');
        $user_data['couple_identity'] = self::get_data('couple_identity');
        $user_data['children_num'] = self::get_data('children_num');

        //开启事物
        IDb::dbStartTrans();

        //同步个人信息
        $result = User::syncUserInfoToMaterial($user_id,$user_data);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::ERR_0004);
            self::set_msg("贷款产品申请失败！");
            self::send();
        }

        //设置贷款产品申请日志
        $loan_apply_where['loan_apply_id'] = $loan_apply_id;
        $loan_apply_where['user_id'] = self::$uid;
        $loan_apply_data['loan_material_status'] = LoanMaterialStatus::release;
        $loan_apply_add = IDb::getInstance("loan_apply")->setDbWhere($loan_apply_where)->setDbData($loan_apply_data)->upd();
        if($loan_apply_add === false){
            IDb::dbRollback();
            self::set_code(self::ERR_0007);
            self::set_msg("贷款产品申请失败！");
            self::send();
        }

        //设置同意过贷款协议
        $post_body = $user_data;
        $post_body['loan_apply_id'] = $loan_apply_id;
        $result = User::agree_protocol($user_id,$post_body,$phone_info,self::$oid);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::ERR_0008);
            self::set_msg("贷款产品申请失败！");
            self::send();
        }

        //取得用户基础信息
        $user_info = IUserCache::getInfo($user_id);
        if(empty($user_info['user_id'])){
            self::set_code(self::WARNING_0003);
            self::set_msg("获取用户基础信息失败！");
            self::send();
        }

        $user_info = User::filterUserInfo($user_info);

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：取得贷款申请记录
     * @开发者：cxl
     */
    public function loan_apply_list(){
        //接收参数
        $user_id  = self::$uid;
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $loan_apply_where['user_id'] = $user_id;
        if(!empty($bottom_id)) $loan_apply_where['loan_apply_id'] = array("lt",$bottom_id);
        $loan_apply_list = IDb::getInstance("loan_apply")
            ->setDbFiled("loan_apply_id,loan_product_id,apply_money,apply_period,loan_apply_status,loan_apply_time,loan_audit_time,loan_audit_note")
            ->setDbWhere($loan_apply_where)
            ->setDbOrder("loan_apply_id desc")
            ->setDbLimit(10)
            ->sel();

        //判断列表是否存在
        if(empty($loan_apply_list) || !is_array($loan_apply_list) || count($loan_apply_list) <= 0){
            $loan_apply_list = array();
        }

        //整理数据
        foreach($loan_apply_list as &$data){
            //设置查询条件
            $loan_product_info = ILoanCache::getInfo($data['loan_product_id']);
            if(empty($loan_product_info['loan_product_id'])){
                self::set_code(self::WARNING_0002);
                self::set_msg("取得贷款申请记录失败！");
                self::send();
            }

//            //设置贷款申请状态
//            switch($data['loan_apply_status']){
//                case LoanApplyStatus::apply:
//                    $data['loan_apply_status_step'] = "0";
//                    $data['loan_apply_status_name'] = "申请已提交";
//                    break;
//                case LoanApplyStatus::audit_pass:
//                    $data['loan_apply_status_step'] = "2";
//                    $data['loan_apply_status_name'] = "材料审核通过";
//                    break;
//                case LoanApplyStatus::audit_failure:
//                    $data['loan_apply_status_step'] = "2";
//                    $data['loan_apply_status_name'] = "材料审核失败";
//                    break;
//                case LoanApplyStatus::confirm_loan:
//                case LoanApplyStatus::brokerage_money:
//                case LoanApplyStatus::finance_brokerage_money:
//                case LoanApplyStatus::apply_bak:
//                    $data['loan_apply_status_step'] = "3";
//                    $data['loan_apply_status_name'] = "申请成功";
//                    break;
//                default:
//                    self::set_code(self::WARNING_0004);
//                    self::set_msg("取得贷款申请记录失败！");
//                    self::send();
//                    break;
//            }

            //设置贷款机构名称
            $data['apply_money'] = number_format($data['apply_money'], 2);
            $data['loan_product_name'] = "{$loan_product_info['loan_agency_name']}-{$loan_product_info['loan_product_name']}";
//            $data['loan_agency_thumb'] = $loan_agency_info['thumb'];
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_apply_list",$loan_apply_list);
        self::send();
    }

    /**
     * @功能：取得贷款申请详情
     * @开发者：cxl
     */
    public function loan_apply_info(){
        //接收参数
        $user_id  = self::$uid;
        $loan_apply_id  = self::get_data('loan_apply_id');

        //设置查询条件
        $loan_apply_where['user_id'] = $user_id;
        $loan_apply_where['loan_apply_id'] = $loan_apply_id;
        $loan_apply_info = IDb::getInstance("loan_apply")
            ->setDbFiled("loan_apply_id,loan_product_id,apply_money,apply_period,loan_apply_status,loan_apply_time,loan_audit_time,loan_audit_note")
            ->setDbWhere($loan_apply_where)
            ->row();

        //判断列表是否存在
        if(empty($loan_apply_info) || !is_array($loan_apply_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得贷款申请记录失败！");
            self::send();
        }

        //设置查询条件
        $loan_product_info = ILoanCache::getInfo($loan_apply_info['loan_product_id']);
        if(empty($loan_product_info['loan_product_id'])){
            self::set_code(self::WARNING_0002);
            self::set_msg("取得贷款申请记录失败！");
            self::send();
        }


        //设置贷款机构名称
        $loan_apply_info['apply_money'] = number_format($loan_apply_info['apply_money'], 2);
        $loan_apply_info['loan_product_name'] = "{$loan_product_info['loan_agency_name']}-{$loan_product_info['loan_product_name']}";
//        $loan_apply_info['loan_agency_thumb'] = $loan_agency_info['thumb'];

        $loan_apply_info['agent_admin_info'] = IAgentAdminCache::getInfoForDisplay($loan_product_info['agent_admin_id']);

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_apply_info",$loan_apply_info);
        self::send();
    }

    /**
     * @功能：贷款产品进度查询日志
     * @开发者：cxl
     */
    public function loan_apply_log(){
        //接收参数
        $loan_apply_id  = self::get_data('loan_apply_id',self::NOT_EMPTY,"贷款产品申请编码不能为空！");
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $loan_apply_log_where['loan_apply_id'] = $loan_apply_id;
        $loan_apply_log_where['is_track'] = IsTrack::Yes;
        if(!empty($bottom_id)) $loan_apply_log_where['loan_apply_log_id'] = array("lt",$bottom_id);
        $loan_apply_log_list = IDb::getInstance("loan_apply_log")
            ->setDbFiled("loan_apply_log_id,operate_note,operate_time")
            ->setDbWhere($loan_apply_log_where)
            ->setDbOrder("loan_apply_log_id desc")
            ->setDbLimit(10)
            ->sel();

        foreach ($loan_apply_log_list as $key=>$value){
            $loan_apply_log_list[$key]['video_list'] = array();
            $loan_apply_log_list[$key]['audio_list'] = array();
            $loan_apply_log_list[$key]['img_list'] = array();
        }

        //判断列表是否存在
        if($loan_apply_log_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("贷款产品进度查询日志失败！");
            self::send();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("loan_apply_log",$loan_apply_log_list);
        self::send();
    }
}
